package Name;

import javafx.print.Collation;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author lijunhao
 * 2023/7/17
 * @description TODO
 */
public class TestClass {
    public static void main(String[] args) {
        List<User> userList = new ArrayList<>();

        User user1 = new User("John", 25);
        User user2 = new User("Alice", 30);
        User user3 = new User("John", 35);
        User user4 = new User("Alice", 28);
        User user5 = new User("Bob", 30);

        addUser(userList, user1);
        addUser(userList, user2);
        addUser(userList, user3);
        addUser(userList, user4);
        addUser(userList, user5);

        for (User user : userList) {
            System.out.println(user);
        }
    }

    public static void addUser(List<User> userList, User newUser) {
        for (User user : userList) {
            if (user.getName().equals(newUser.getName()) && user.getAge() == newUser.getAge()) {
                return; // 如果姓名和年龄都相同，则不进行存储
            }
        }

        userList.add(newUser);

        Collections.sort(userList, new Comparator<User>() {

            public int compare(User user1, User user2) {
                if (user1.getName().equals(user2.getName())) {
                    return user2.getAge() - user1.getAge(); // 姓名相同，按照年龄降序排序
                } else {
                    return user1.getName().compareTo(user2.getName()); // 姓名不相同，按照姓名升序排序
                }
            }
        });
    }
}
